/**
 * @Description SalesmanAchievementsController.java
 * @author xu_cc [18724000100@163.com]
 * @date 创建时间：2018年1月3日 下午2:51:24
 */
package com.solution.admin.report.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.auth.entity.OrgEntity;
import com.solution.admin.auth.service.OrgService;
import com.solution.admin.report.entity.SalesmanAchievementsEntity;
import com.solution.admin.report.service.SalesmanAchievementsService;
import com.solution.common.controller.BaseController;
import com.solution.common.utils.DateUtils;
import com.solution.common.utils.string.StringUtil;

/**
 * 业务员业绩报表
 * 
 * @author xu_cc
 * @date 创建时间：2018年1月3日 下午2:52:12
 */
@RequestMapping("/report")
@Controller
public class SalesmanAchievementsController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(SalesmanAchievementsController.class);

	@Autowired
	OrgService orgService;

	@Autowired
	SalesmanAchievementsService salesmanAchievementsService;

	/**
	 * 转业务员业绩页面
	 * 
	 * @param request
	 * @param model
	 * @return String
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2018年1月3日 下午3:02:40
	 */
	@RequestMapping("/toSalesmanAchievementsPage")
	public String toSalesmanAchievementsPage(HttpServletRequest request, Model model) {
		try {
			// 上个月(开始日期)
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, -1);
			String beginDate = DateUtils.formatDate(calendar.getTime());
			// 当前日期（结束日期）
			String endDate = DateUtils.getDate();
			model.addAttribute("beginDate", beginDate);
			model.addAttribute("endDate", endDate);
			List<OrgEntity> orgList = orgService.listOrg(new HashMap<String, Object>());
			model.addAttribute("orgList", orgList);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return "report/salesman_achievements";
	}

	/**
	 * 业务员业绩数据获取
	 * 
	 * @param request
	 * @param response
	 * @param entity
	 * @return Map<String,Object>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2018年1月3日 下午3:12:06
	 */
	@RequestMapping("/getSalesmanAchievementsData")
	@ResponseBody
	public List<SalesmanAchievementsEntity> getSalesmanAchievementsData(HttpServletRequest request, HttpServletResponse response,
	        SalesmanAchievementsEntity entity) {
		try {
			if (null == entity) {
				entity = new SalesmanAchievementsEntity();
			}
			// 默认按照订单金额排序
			if (StringUtil.isEmpty(entity.getOrderBy())) {
				entity.setOrderBy(" totalAmount ");
			}
			List<SalesmanAchievementsEntity> list = salesmanAchievementsService.listByParam(entity);
			if (null != list && list.size() > 0) {
				//重新排序
				if("totalAmount".equals(entity.getOrderBy().trim())) {
					//订单金额
					//降序
					Collections.sort(list, new Comparator<SalesmanAchievementsEntity>() {
						public int compare(SalesmanAchievementsEntity p1, SalesmanAchievementsEntity p2) {
							return p2.getTotalAmount().compareTo(p1.getTotalAmount());
						}
					});
				} else {
					//订单数量
					//降序
					Collections.sort(list, new Comparator<SalesmanAchievementsEntity>() {
						public int compare(SalesmanAchievementsEntity p1, SalesmanAchievementsEntity p2) {
							return p2.getOrderCount().compareTo(p1.getOrderCount());
						}
					});
				}
			} else {
				list = new ArrayList<SalesmanAchievementsEntity>();
				SalesmanAchievementsEntity temp = new SalesmanAchievementsEntity();
				temp.setUserName("暂无数据");
				temp.setOrgName("暂无数据");
				temp.setOrderCount(0);
				temp.setTotalAmount(BigDecimal.ZERO);
				list.add(temp);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 部门业绩数据获取
	 * 
	 * @param request
	 * @param response
	 * @param entity
	 * @return Map<String,Object>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2018年1月3日 下午3:12:06
	 */
	@RequestMapping("/getOrgSalesData")
	@ResponseBody
	public List<SalesmanAchievementsEntity> getOrgSalesData(HttpServletRequest request, HttpServletResponse response,
	        SalesmanAchievementsEntity entity) {
		try {
			List<SalesmanAchievementsEntity> list = salesmanAchievementsService.listOrgByParam(entity);
			//订单金额
			if (null != list && list.size() > 0) {
				//降序
				Collections.sort(list, new Comparator<SalesmanAchievementsEntity>() {
					public int compare(SalesmanAchievementsEntity p1, SalesmanAchievementsEntity p2) {
						return p2.getTotalAmount().compareTo(p1.getTotalAmount());
					}
				});
			} else {
				list = new ArrayList<SalesmanAchievementsEntity>();
				SalesmanAchievementsEntity temp = new SalesmanAchievementsEntity();
				temp.setUserName("暂无数据");
				temp.setOrgName("暂无数据");
				temp.setOrderCount(0);
				temp.setTotalAmount(BigDecimal.ZERO);
				list.add(temp);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

//	/**
//	 * 冒泡排序（倒序）
//	 * 
//	 * @param list
//	 * @return List<SalesmanAchievementsEntity>
//	 * @author xu_cc
//	 * @throws Exception
//	 *             exception
//	 * @Date 创建时间：2018年1月5日 下午6:07:22
//	 */
//	public static List<SalesmanAchievementsEntity> descSort(List<SalesmanAchievementsEntity> list) {
//		try {
//			SalesmanAchievementsEntity temp = null;
//			if (null != list && list.size() > 0) {
//				for (int i = 0; i < list.size(); i++) {
//					for (int j = 0; j < list.size() - 1; j++) {
//						if (list.get(j).getTotalAmount().compareTo(list.get(j + 1).getTotalAmount()) == -1) {
//							temp = list.get(j);
//							list.set(j, list.get(j + 1));
//							list.set(j + 1, temp);
//						}
//					}
//				}
//			} else {
//				List<SalesmanAchievementsEntity> tempList = new ArrayList<SalesmanAchievementsEntity>();
//				temp = new SalesmanAchievementsEntity();
//				temp.setUserName("暂无数据");
//				temp.setOrgName("暂无数据");
//				temp.setOrderCount(0);
//				temp.setTotalAmount(BigDecimal.ZERO);
//				tempList.add(temp);
//				return tempList;
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
//		return list;
//	}

}
